package io.bsly.modules.sys.controller;

import io.bsly.common.annotation.SysLog;
import io.bsly.common.utils.PageUtils;
import io.bsly.common.utils.R;
import io.bsly.modules.app.entity.localEntity.DyCouponEntity;
import io.bsly.modules.app.service.localmaster.DyCouponService;
import io.bsly.modules.app.service.localmaster.DyOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;

@Api(tags = "后台审核券码")
@RestController
@RequestMapping("/sys/check")
public class SysCheckController {

    @Autowired
    DyCouponService dyCouponService;
    @Resource
    DyOrderService dyOrderService;


    /**
     * 列表
     */
    @ApiOperation(value="券列表", notes = "抖音兑换券，入参为map类型")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username",value = "用户名" , dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "dyOrderCode",value = "抖音订单号" , dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "createDate",value = "申请时间Date" , dataType = "string", paramType = "query",example = "2021-09-28"),
            @ApiImplicitParam(name = "createTime",value = "申请时间Time" , dataType = "string", paramType = "query",example = "2021-09-28 14:14:14"),
            @ApiImplicitParam(name = "productName",value = "品名" , dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "productId",value = "卡Id" , dataType = "int", paramType = "query"),

            @ApiImplicitParam(name = "status",value = "状态值,多个用逗号分隔" , dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "userId",value = "用户ID" , dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "page",value = "当前页数" , dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "limit",value = "每页条数" , dataType = "string", paramType = "query")
    })
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> map, HttpServletRequest request){
        PageUtils pageUtils = dyCouponService.queryPage(map);
        return R.ok().put("page", pageUtils);
    }
    /**
     * 修改
     */
    @SysLog(value="审核核销码")
    @ApiOperation(value="审核", notes = "根据ID进行更新")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "表id" , dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "status",value = "审核状态,1 待激活 2激活 3无效使用完次数，4激活失败" , dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "refuseReason",value = "拒绝理由" , dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "productId",value = "产品id" , dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "dyOrderCode",value = "抖音订单" , dataType = "string", paramType = "query")
    })
    @PostMapping("/coupon")
    public R couponCheck(@RequestBody DyCouponEntity dyCouponEntity){
        dyCouponEntity.setUpdateTime(new Date());
        dyCouponService.updateById(dyCouponEntity);
        return R.ok();
    }


    /**
     * 强制取消订单
     */
    @SysLog(value="强制取消订单")
    @ApiOperation(value = "根据订单ID取消订单",notes = "重置兑换券状态，并返还间夜数")
    @PostMapping("/forceCancelOrder")
    public R forceCancelOrder(@RequestBody String orderId){
        dyOrderService.cancelOrder(orderId);
        return R.ok();
    }


}
